<template>
    <div class="common-box" v-if="boxIsShow">
        <div class="common-content-box">
            <div class="common-content-top-box">
                <div class="form_box">
                    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                        <el-form-item label="金额" prop="money">
                            <el-input type="number" v-model="ruleForm.money"></el-input>
                        </el-form-item>
                        <el-form-item label="算力数量" prop="point">
                            <el-input type="number" v-model="ruleForm.point"></el-input>
                        </el-form-item>
                        <el-form-item label="排序">
                            <el-input type="number" v-model="ruleForm.level"></el-input>
                        </el-form-item>
                        <el-form-item label="有效期(天)">
                            <el-input type="number" v-model="ruleForm.youxiaoDays"></el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
                            <el-button @click="resetForm('ruleForm')">重置</el-button>
                        </el-form-item>
                    </el-form>
                </div>
            </div>
            <div class="common-content-bottom-box">
                <div class="table_box">
                    <el-table
                        :data="tableData"
                        stripe
                        style="width: 100%">
                        <el-table-column
                        prop="id"
                        label="编号"
                        width="180"
                        align="center">
                        </el-table-column>
                        <el-table-column
                        prop="point"
                        label="算力数量"
                        width="180"
                        align="center">
                        </el-table-column>
                        <el-table-column
                        prop="money"
                        label="金额"
                        width="180"
                        align="center">
                        </el-table-column>
                        <el-table-column
                        label="有效天数"
                        prop="youxiaoDays"
                        align="center"
                        width="180">
                        </el-table-column>
                        <el-table-column
                        prop="level"
                        label="排序"
                        width="180"
                        align="center">
                        </el-table-column>
                        <el-table-column
                        label="状态"
                        align="center"
                        width="180">
                        <template slot-scope="scope">
                            {{ scope.row.state===1?'启用':'禁用' }}
                            <el-button @click="updateState(scope.row.id,scope.row.state)" type="text" size="medium">修改</el-button>
                        </template>
                        </el-table-column>
                        <el-table-column
                        label="操作"
                        align="center">
                        <template slot-scope="scope">
                            <el-button @click="getDesc(scope.row.id)" type="primary" size="medium">编辑</el-button>
                            <el-button @click="del(scope.row.id)" type="danger" size="medium">删除</el-button>
                        </template>
                        </el-table-column>
                    </el-table>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
import { UPLOAD_URL } from '@/utils/http.js';
export default {
  data() {
    return {
        uploadNum:1,
        UPLOAD_URL,
        boxIsShow:false,
        ruleForm: {
            id:0,
            money: '',
            point: '',
            level: 1,
            youxiaoDays: 9999
        },
        rules: {
            money: [
                { required: true, message: '请输入金额', trigger: ["blur,change"] }
            ],
            point: [
                { required: true, message: '请输入所得积分数', trigger: ["blur,change"] }
            ]
        },
        tableData:[]
    };
  },
  created() {
    
  },
  mounted(){
    this.__init();
  },
  methods: {
    __init() {
      this.getDataList();
    },
    getDataList(){
      var url="manager/geChargeInfoList";
      var params={}
      this.$post(url, params).then((res) => {
        console.log(res)
        if (res.code === 200) {
          this.tableData=res.data;
        } else {
          this.$message.error(res.msg)
        }
      });
    },
    submitForm(formName) {
        this.$refs[formName].validate((valid) => {
            if (valid) {
                var url="manager/editChargeInfo";
                var params={
                    ...this.ruleForm
                }
                this.$post(url, params).then((res) => {
                    console.log(res)
                    if (res.code === 200) {
                        this.$message.success('操作成功')
                        this.getDataList();
                        this.resetForm('ruleForm');
                    } else {
                        this.$message.error(res.msg)
                    }
                });
            } else {
                console.log('error submit!!');
                return false;
            }
        });
    },
    resetForm(formName) {
        this.$refs[formName].resetFields();
        this.ruleForm.youxiaoDays=9999;
        this.ruleForm.level=1;
        this.ruleForm.id=0;
    },
    del(id){
        this.$confirm('此操作将永久删除该选项, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
            var url="manager/delChargeInfoById";
            var params={
                id:id
            }
            this.$post(url, params).then((res) => {
                if (res.code === 200) {
                    this.$message.success('删除成功')
                    this.getDataList();
                } else {
                    this.$message.error(res.msg)
                }
            });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    getDesc(id){
        var url="manager/getChargeInfoDesc";
        var params={
            id:id
        }
        this.$post(url, params).then((res) => {
            if (res.code === 200) {
                var dataDesc=res.data;
                this.ruleForm={
                    id:dataDesc.id,
                    money: dataDesc.money,
                    point: dataDesc.point,
                    level: dataDesc.level,
                    youxiaoDays: dataDesc.youxiaoDays
                }
            } else {
                this.$message.error(res.msg)
            }
        });
    },
    updateState(id,state){
        if(state==1){
        state=2
      }else{
        state=1
      }
      this.$confirm('是否修改此选项状态?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
            var url="manager/updateChargeInfoState";
            var params={
                id:id,
                state:state
            }
            this.$post(url, params).then((res) => {
                if (res.code === 200) {
                    this.$message.success('操作完成');
                    this.getDataList();
                } else {
                    this.$message.error(res.msg)
                }
            });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });          
        });
    }
  }
}
</script>
<style scoped lang="scss">
  .common-box{
    background-color: #f9f9fb;
    height: 100%;
    position: relative;
  }
  .common-content-box{
    height: 95%;
    margin-top: 2%;
    width: 96%;
    display: inline-block;
  }
  .common-content-top-box{
    width: 100%;
    background-color: #fff;
    min-height: 200px;
    box-shadow: 1px 1px 6px 2px rgba(0,0,0,0.1);
    border-radius: 5px;
    text-align: left;
  }
  .form_box{
    margin-left: 20px;
    margin-top: 25px;
    width: 40%;
    display: inline-block;
  }
  .common-content-bottom-box{
    width: 100%;
    background-color: #fff;
    min-height: 200px;
    box-shadow: 1px 1px 6px 2px rgba(0,0,0,0.1);
    border-radius: 5px;
    margin-top: 30px;
    padding: 20px 0;
  }
  .table_box{
    width: 96%;
    margin-left: 2%;
  }
</style>